package com.umeox.iqibla.custom_tasbih.ui

import android.animation.Animator
import android.animation.Animator.AnimatorListener
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import android.widget.SeekBar
import androidx.core.graphics.toColorInt
import com.airbnb.lottie.LottieAnimationView
import com.umeox.iqibla.custom_tasbih.R
import com.umeox.iqibla.custom_tasbih.data.TasbihTaskListManager
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity
import com.umeox.moto.watch.themes.app.ThemeManager
import com.umeox.moto.watch.themes.widget.TextView

class TaskActivity : ThemeAppCompatActivity() {

    companion object {
        private const val INTENT_KEY_TASK_INDEX = "index"

        fun intoTaskActivity(context: Context, taskIndex: Int) {
            context.startActivity(Intent(context, TaskActivity::class.java).apply {
                putExtra(INTENT_KEY_TASK_INDEX, taskIndex)
            })
        }
    }

    private var progressValue = 0
    private var tasbihIndex = 0
    private var isUpdate = false
    private var startTime = 0L

    @SuppressLint("SetTextI18n", "UseCompatLoadingForDrawables")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_task)

        tasbihIndex = intent?.getIntExtra(INTENT_KEY_TASK_INDEX, 0)!!

        val task = TasbihTaskListManager.getTaskByIndex(tasbihIndex)

        if (task == null) {
            finish()
        }

        val tvContent = findViewById<TextView>(R.id.tv_content)
        val tvDesc = findViewById<TextView>(R.id.tv_desc)
        tvContent.text = task!!.title
        tvDesc.text = task.content

        val sbProgress = findViewById<SeekBar>(R.id.sb_progress)
        val tvProgress: TextView = findViewById(R.id.tv_progress)
        progressValue = task.additionalInfo.completed
        sbProgress.isEnabled = false
        sbProgress.max = task.additionalInfo.goals
        sbProgress.progress = task.additionalInfo.completed
        tvProgress.text =
            "${task.additionalInfo.completed}/${task.additionalInfo.goals}"
        val llRoot = findViewById<LinearLayout>(R.id.llRoot)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (ThemeManager.getInstance().currentTheme == 0) {
                //暗色
                sbProgress.progressDrawable =
                    getDrawable(R.drawable.custom_progress_bar)
                llRoot.background.setTint("#CC393A3E".toColorInt())
            } else {
                //亮色
                sbProgress.progressDrawable =
                    getDrawable(R.drawable.custom_progress_bar_light)
                llRoot.background.setTint("#CCE7F3EB".toColorInt())
            }
        }

        val lottieAnimationView = findViewById<LottieAnimationView>(R.id.lottieAnimationView)
        //判断任务是否已经完成
        if (task.additionalInfo.completed == task.additionalInfo.goals) {
            lottieAnimationView.visibility = View.GONE
        } else {
            lottieAnimationView.setOnClickListener {
                if (!lottieAnimationView.isAnimating) {
                    lottieAnimationView.playAnimation()
                }
            }
            lottieAnimationView.addAnimatorListener(object : AnimatorListener {
                override fun onAnimationStart(animation: Animator?) {

                }

                override fun onAnimationEnd(animation: Animator?) {
                    isUpdate = true
                    startTime = System.currentTimeMillis()
                    progressValue++
                    sbProgress.progress = progressValue
                    tvProgress.text =
                        "${progressValue}/${task.additionalInfo.goals}"
                    if (progressValue == task.additionalInfo.goals) {
                        //完成赞念任务
                        finishTaskByIndex(tasbihIndex, task.additionalInfo.goals)
                    }
                }

                override fun onAnimationCancel(animation: Animator?) {
                }

                override fun onAnimationRepeat(animation: Animator?) {
                }
            })
        }
    }

    private fun finishTaskByIndex(index: Int, goals: Int) {
        TasbihTaskListManager.updateTaskProgressByIndex(index, goals)
        submit()
        //关闭此页面 跳转完成页面
        FinishTaskActivity.intoFinishTaskActivity(this, goals)
        finish()
    }

    override fun onBackPressed() {
        if (isUpdate) {
            TasbihTaskListManager.updateTaskProgressByIndex(tasbihIndex, progressValue)
            submit()
        }
        super.onBackPressed()
    }

    private fun submit() {
        TasbihTaskListManager.submitTaskRecordDTOByIndex(tasbihIndex, startTime)
    }

}